package com.carey.blog.framework.latke.jsonrpc.impl;

import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;


import com.carey.blog.framework.latke.Keys;
import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.framework.latke.jsonrpc.AbstractJSONRpcService;
import com.carey.blog.framework.latke.service.LangPropsService;
import com.carey.blog.framework.latke.service.ServiceException;
import com.carey.blog.framework.latke.util.Locales;

/**
 * Language service for JavaScript client.
 * 
 */
public final class LanguageService extends AbstractJSONRpcService {

	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger.getLogger(LanguageService.class
			.getName());
	/**
	 * Language service.
	 */
	private LangPropsService langPropsService = LangPropsService.getInstance();

	/**
	 * Gets locale of the specified request.
	 * 
	 * @param request
	 *            the specified
	 * @return for example:
	 * 
	 *         <pre>
	 * {
	 *      "locale": "zh_CN"
	 * }
	 * </pre>
	 * @throws ActionException
	 *             action exception
	 */
	public JSONObject getLocale(final HttpServletRequest request)
			throws ActionException {
		final Locale locale = Locales.getLocale(request);
		final JSONObject ret = new JSONObject();

		try {
			ret.put(Keys.LOCALE, locale);
		} catch (final JSONException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);

			throw new ActionException(e);
		}

		return ret;
	}

	/**
	 * Gets all labels for multi-languages by locale of the specified request
	 * json object.
	 * 
	 * @param requestJSONObject
	 *            the specified request json object, for example:
	 * 
	 *            <pre>
	 * {
	 *     "locale": "zh_CN"
	 * }
	 * </pre>
	 * @param request
	 *            the specified http servlet request
	 * @return for example:
	 * 
	 *         <pre>
	 * {
	 *     "htmlTitle": "Co-soft",
	 *     "labels": [
	 *         {
	 *             "labelId": "companyAddress",
	 *             "labelText": "公司地址"
	 *         }, ....
	 *     ],
	 *     "sc": "CHANGE_LOCALE_SUCC"
	 * }
	 * </pre>
	 * @throws ActionException
	 *             action exception
	 */
	public JSONObject getLabels(final JSONObject requestJSONObject,
			final HttpServletRequest request) throws ActionException {
		try {
			final String localeString = requestJSONObject
					.getString(Keys.LOCALE);
			final Locale locale = new Locale(Locales.getLanguage(localeString),
					Locales.getCountry(localeString));

			Locales.setLocale(request, locale);

			final JSONObject ret = langPropsService.getLabels(locale);
			LOGGER.log(Level.FINER,
					"Got all labels[locale={0}, sessionId={1}]", new Object[] {
							locale, request.getSession().getId() });

			return ret;
		} catch (final ServiceException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ActionException(e);
		} catch (final JSONException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ActionException(e);
		}
	}
}
